#include "EpilExpressionListParser.h"
#include "EpilExpressionParser.h"
#include "EpilPointerHelper.h"


EpilExpressionListParser::EpilExpressionListParser( EpilTokenStream & tokens )
: EpilSyntaxParserBase( tokens )
{
}


void EpilExpressionListParser::Parse()
{
	// explist1 -> expr { `,' expr }

	// Parse the first expression.
	EpilExpressionParser headExprParser( this->GetTokens() );
	headExprParser.Parse();
	EpilExpressionCodeBase * headExpr = headExprParser.GetExpression();
	m_expressions.push_back( headExpr );

	while( this->GetTokens().GetCurrentToken().type == ETT_OpComma )
	{
		this->GetTokens().MoveToNext();

		EpilExpressionParser exprParser( this->GetTokens() );
		try
		{
			exprParser.Parse();
		}
		catch( ... )
		{
			EpilPointerHelper::DeleteItemsAndClear( m_expressions );
			throw;
		}
		EpilExpressionCodeBase * expr = exprParser.GetExpression();
		m_expressions.push_back( expr );
	}
}